<?php
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
/**
 * 商品控制器
 * @author Fosf
 * @version 1.0
 * @created 2011-04-11
 */
class products extends general
{
    function __construct()
    { // 公用
        parent::__construct(); // 这是必须的
        $this->isproducts = true;
        $this->isCurrencies = true;
    }
    /**
     * 商品分类
     */
    function index()
    {
        $alias = urldecode($this->spArgs('alias', false));
        spClass('Safe')->StopAttack('alias',$alias);
        //空别名就返回404
        if (!$alias) {
            $this->isproductsall = true;
            $title = T("All Products");
            $keywords = KEYWORDS;
            $description = DESCRIPTION;
            $conditions = '1=1';
            $this->categories_name = T('All Products');
        } else {
            $this->isproductslist = true;
            $categoriesobj = spClass("categoriesModel");
            $request = $categoriesobj->FindformAlias($alias); //通过别名获取当前类信息
            //别名不存在就返回404
            if (!$request)
                ERROR404(2);

            $title = $request['detail']['categories_title'];
            $keywords = $request['detail']['categories_keywords'];
            $description = $request['detail']['categories_meta_description'];
            $conditions = array('master_categories_id' => $request['categories_id']);
            $this->categories_id = $categories_id = $request['categories_id'];
            $this->categories_name = $request['detail']['categories_name'];
            $this->son = $son = $categoriesobj->spCache(3600 * 24)->Categories_son($categories_id);
        }
        //如果不存在子类
        if (!isset($son)) {
            $order = $this->spArgs("order", 2); //排序
            $current_page = $this->spArgs("page", 1);
            $page_size = ALLCP;
            $disporder = array(
                '4' => T('Price - high to low'),
                '5' => T('Price - low to high'),
                '2' => T('Date Added - New to Old'),
                '3' => T('Date Added - Old to New'),
                );
            $this->disporder = $disporder;
            $this->order = $order;
            $productsobj = spClass("productsModel");
            $request = $productsobj->spCache(3600 * 24)->products_all($conditions, $current_page,
                $page_size, $order);
            $this->productslist = $request['productslist'];
            $this->pager = $pager = $request['pager'];

        }
        $this->alias = $alias;
        $this->title = $title;
        $this->keywords = $keywords;
        $this->description = $description;
        $this->display("index/index_products_all.html");
    }

    function search()
    {
        $this->isproductssearch = true;
        $productsdescriptionobj = spClass("products_descriptionModel");


        $str = urldecode($this->spArgs('alias', false));
        spClass('Safe')->StopAttack('alias',$str);
        if (!$str)
            ERROR404(2);

        $str = Qkg($str); //将特殊字符转成空格

        $sqlstr = 'products_seokeywords like ' . $productsdescriptionobj->escape('%' . $str .
            '%') . ' or products_description like ' . $productsdescriptionobj->escape('%' .
            $str . '%') . ' or products_name like ' . $productsdescriptionobj->escape('%' .
            $str . '%');


        $conditions = $sqlstr; //主表单条件

        $current_page = $this->spArgs("page", 1);
        $page_size = SOCP;

        $request = $productsdescriptionobj->Products_list_TAG_SEARCH($conditions, $current_page,
            $page_size);
        //dump($request['productslist']);
        $this->productslist = $request['productslist'];
        $this->pager = $request['pager'];

        $this->alias = $str;
        $this->Fnav = 'SEARCH:' . $str;
        $this->title = "SEARCH:" . $str;
        $this->keywords = $str;
        $this->description = "SEARCH:" . $str;
        $this->display("index/index_products_search.html");
    }
    function view()
    {
        $this->isproductsview = true;
        $alias = urldecode($this->spArgs('alias', false));
        spClass('Safe')->StopAttack('alias',$alias);
        if (!$alias)
            ERROR404(1);
        $products = spClass('productsModel')->FindformAlias($alias);
        if (!$products)
            ERROR404(1);
        spClass('productsModel')->hits($products['products_id']); //统计次数
        $action = $this->spArgs("action");

        //是否加入此商品到购和车
        if ($action == "add_product") {
            //exit;
            //添加购物车
            $cart_quantity = $this->spArgs("cart_quantity", 1);
            $attribsize = $this->spArgs("attribsize");
            $products_id = $products['products_id'];
            $products_name = $products['detail']['products_name'];
            //$priceview = ($products['products_price_discounted'] <> 0) ? $products['products_price_discounted'] : $products['products_price'];
            //$priceview = Get_price(array('pid'=>$products_id,'price'=>$products['products_price'],'price2'=>$products['products_price_discounted'],'strtype'=>1,'GDcart'=>5));
            $customers_id = $this->userarr['customers_id']; //此数据来自初始化处
            if ($customers_id <> '') {
                $basket = spClass('customers_basketModel')->basketExist($customers_id, $products_id,
                    $attribsize);
                //dump($basket);

                if ($basket['customers_basket_id'] <> '') {
                    $condition2 = array('customers_basket_id' => $basket['customers_basket_id']);
                    spClass('customers_basketModel')->incrField($condition2,
                        'customers_basket_quantity', $cart_quantity);
                    //exit;
                } else {
                    $data = array(
                        'customers_id' => $customers_id,
                        'products_id' => $products_id,
                        'products_name' => $products_name,
                        'customers_basket_quantity' => $cart_quantity,
                        'attributes' => $attribsize,
                        'final_price' => $products['products_price'],
                        'discounted_price' => $products['products_price_discounted'],
                        'customers_basket_date_added' => TIME());

                    spClass('customers_basketModel')->create($data);
                }
            } else {
                $cartid = rand(10, 100) + time();
                $cart = $this->cartarr;
                $cart->addCart($cartid, $products_id, $products_name, $attribsize, $products['products_price'],
                    $products['products_price_discounted'], '', $cart_quantity);
                //echo $ddd;
            }
            $this->jump(spUrl('main', 'ShoppingCart'));
        } else {
            $this->products = $products;
            $this->categories = spClass('categoriesModel')->FindformID($products['master_categories_id']);
            $this->categories_id = $products['master_categories_id'];
            $allimg = unserialize($products['products_allimg']);
            foreach ($allimg as $k => $v) {
                if ($v == $products['products_image'])
                    unset($allimg[$k]);
            }
            $this->allimg = $allimg;

            if (PL == '1')
                $this->Commentlist = spClass('commentsModel')->GetCommentsList($products['products_id']);
            $this->alias = $alias;
            $this->title = $products['detail']['products_title'];
            $this->keywords = $products['detail']['products_seokeywords'];
            $this->description = $products['detail']['products_seodescription'];
            $this->display("index/index_product_view.html");
        }
    }

    //保存新闻信息评论
    function savecomment()
    {
        //dump($this->spArgs());
        $products_id = $this->spArgs("commentproducts_id");
        $action = $this->spArgs("action");
        $rating = $this->spArgs("rating", 5);
        $review_text = urldecode(strip_tags($this->spArgs("review_text")));
        spClass('Safe')->StopAttack('review_text',$review_text,2);
        $ip = getIP();

        if (isset($review_text) and isset($products_id)) {
            if (isset($this->userarr['customers_id'])) {
                $customers_id = $this->userarr['customers_id'];
            } else {
                $customers_id = 0;
            }
            if (PL == 0) {
                exit();
            } else {
                if (YKPL == 0 && $customers_id == 0) {
                    exit();
                }
            }


            $data = array(
                "products_id" => $products_id,
                "rating" => $rating,
                "review_text" => $review_text,
                "customers_id" => $customers_id,
                "ip" => $ip,
                "date_add" => time(),
                "type" => 0);
            $Obj = spClass('commentsModel');

            if ($action == 'process') {

                if ($Obj->SaveComment($data)) {
                    $msg = T("Reviews may require prior approval before they will be displayed!");
                    $status = 'y';
                } else {
                    $msg = T("Error comment failed!");
                    $status = 'n';
                }


            } else {
                $msg = T("Error comment failed!");
                $status = 'n';
            }
        } else {
            $msg = T("Error comment failed!");
            $status = 'n';
        }
        $result = array(
            'status' => $status,
            'info' => $msg,
            );
        echo json_encode($result); // 返回（显示）JSON结果
    }
}
